home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / weekday.com / WEEKDAY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-25  |  3.7 KB  |  135 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int Day_Of_Week();
  5.  
  6. main(int argc, char *argv[])
  7. {
  8.  
  9.     int weekday;
  10.  
  11.     if(argc != 4)
  12.     {
  13.     printf("\nSyntax:  WEEKDAY day month year  (ie WEEKDAY 23 5 1990)\n");
  14.     }
  15.     else
  16.     {
  17.     weekday = Day_Of_Week(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));
  18.  
  19.     switch(weekday)
  20.     {
  21.         case 1 : printf("Sunday\n");
  22.              break;
  23.         case 2 : printf("Monday\n");
  24.              break;
  25.         case 3:  printf("Tuesday\n");
  26.              break;
  27.         case 4:  printf("Wednesday\n");
  28.              break;
  29.         case 5:  printf("Thursday\n");
  30.              break;
  31.         case 6:  printf("Friday\n");
  32.              break;
  33.         case 7:  printf("Saturday\n");
  34.              break;
  35.        default:  printf("Invalid date specified\n");
  36.              printf("Day = %s, Month = %s, Year = %s\n",argv[1], argv[2], argv[3]);
  37.              break;
  38.     }
  39.     }
  40. }
  41.  
  42. /***************************************************************************/
  43. int Day_Of_Week(int day, int month, int year)
  44. /***************************************************************************/
  45. /*
  46.     FUNCTION:  Day_Of_Week
  47.  
  48.  DESCRIPTION:  This function returns the day of week given the month, day
  49.            and year.
  50.  
  51.       INPUT:  day, month, year - integer values for each with year being
  52.           the full 4 digit value (1990).  Valid years are between 1 and
  53.           9999 although, given the iterations of our calendar system the
  54.           smaller year values do not make much sense.
  55.  
  56.     RETURNS:  day of week (1-7 = Sunday - Saturday).  0 is returned if
  57.           specified date is invalid (ie 31-FEB-1990)
  58.  
  59. */
  60. {
  61.  
  62. #define DEC 12
  63. #define NOV 11
  64. #define OCT 10
  65. #define SEP 9
  66. #define AUG 8
  67. #define JUL 7
  68. #define JUN 6
  69. #define MAY 5
  70. #define APR 4
  71. #define MAR 3
  72. #define FEB 2
  73. #define JAN 1
  74.  
  75.     long unsigned day_count;
  76.     int leap_year, week_day;
  77.  
  78.     /* Calculate number of days since 01-JAN-0001 */
  79.  
  80.     if((year > 1) && (year <= 9999))   /* is the year range valid? */
  81.     {
  82.     day_count = (year * 365);
  83.     day_count += ((year-1)/4);    /* take into account leap years */
  84.     day_count -= ((year-1)/100);  /* subtract non-leap years (every 100) */
  85.     day_count += ((year-1)/400);  /* add leap years (every 400) */
  86.  
  87.     /* Leap years occur every 4 years except years divisable by 100.  The */
  88.     /* exception are the years divisable by 400 which are leap years */
  89.  
  90.     leap_year = ((((year % 4) == 0) && ((year % 100) != 0)) || (year % 400 == 0));
  91.  
  92.     if((month >= 1) && (month <= 12))
  93.     {
  94.         switch(month)
  95.         {
  96.         /* add all days up to current month */
  97.  
  98.         case DEC : day_count += 30;
  99.                if((month == DEC)&&(day > 31)) return(0);
  100.         case NOV : day_count += 31;
  101.                if((month == NOV)&&(day > 30)) return(0);
  102.         case OCT : day_count += 30;
  103.                if((month == OCT)&&(day > 31)) return(0);
  104.         case SEP : day_count += 31;
  105.                if((month == SEP)&&(day > 30)) return(0);
  106.         case AUG : day_count += 31;
  107.                if((month == AUG)&&(day > 31)) return(0);
  108.         case JUL : day_count += 30;
  109.                if((month == JUL)&&(day > 31)) return(0);
  110.         case JUN : day_count += 31;
  111.                if((month == JUN)&&(day > 30)) return(0);
  112.         case MAY : day_count += 30;
  113.                if((month == MAY)&&(day > 31)) return(0);
  114.         case APR : day_count += 31;
  115.                if((month == APR)&&(day > 30)) return(0);
  116.         case MAR : day_count += (leap_year) ? 29 : 28;
  117.                if((month == MAR)&&(day > 31)) return(0);
  118.         case FEB : day_count += 31;
  119.                if(((month == FEB)&&(leap_year)&&(day > 29))||
  120.                   ((month == FEB)&&(!leap_year)&&(day > 28))) return(0);
  121.         case JAN : if((month == JAN)&&(day > 31)) return(0);
  122.                if(day < 1) return(0);
  123.                break;
  124.         }
  125.  
  126.         day_count += day;
  127.  
  128.         week_day = (day_count % 7) + 1;
  129.  
  130.         return(week_day);
  131.     }
  132.     else return(0);
  133.     }
  134.     else return(0);
  135. }